// pages/roomsList/roomsList.js
var common = require('../../utils/common.js');
const app = getApp();
var that="";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: '',//状态0为已生成账单，1未生成
    house_id: '',//房屋id
    showtab: 0,  //顶部选项卡索引
    tabnav: {//顶部选项卡数据7
      tabnum: 2,
      tabitem: [
        {
          "id": 0,
          "type": "",
          "text": "已生成账单",
          "count": ''
        },
        {
          "id": 1,
          "type": "A",
          "text": "未生成账单",
          "count": ''
        }
      ]
    },
    dataList: [],
    roomsList:[],
    year:'',
    month:'',
  },

  setTab:function (e) { //设置选项卡选中索引
    const edata = e.currentTarget.dataset;
    this.setData({
      showtab: edata.tabindex,
    })
    if (edata.tabindex == 0) {
      //已租
      this.setData({
        status: '1'
      })
      // if (this.data.forRentDataList.length < 1) {
      //   console.log("已租")
      //   this.getDataList();
      // }
    }
    else if (edata.tabindex == 1) {
      //未租
      this.setData({
        status: '0'
      })
      // if (this.data.waitingForRentDataList.length < 1) {
      //   this.getDataList();
      // }
    }
  },
 
  //点击右边的展开图标
  clickRIcon: function (e) {
    var index = e.currentTarget.dataset.index;
    var res = [];
    if (this.data.showtab == 0) {
      res = this.data.dataList;
      res[index].check = !res[index].check;
      this.setData({
        dataList: res
      })
    } else {
      res = this.data.roomsList;
      res[index].check = !res[index].check;
      this.setData({
        roomsList: res
      })
    }
    // this.data.dataList[index].check = !this.data.dataList[index].check;
    // this.setData({
    //   dataList: this.data.dataList
    // })
  },

  getfeeData: function (year, month, status) {
    common._get('/wx/landlord/charge/month/totalfee/list', { year: year, month: month, status: status },
      //成功
      function (res) {
        console.log(res.data);
        if (res.data.success) {
          for (var i in res.data.data) {
            res.data.data[i].check = false;
          }
          if (status==1){
           that.setData({
             dataList: res.data.data
           })
         }else{
           that.setData({
             roomsList: res.data.data
           })
         }
          wx.stopPullDownRefresh();
        }
      });
  },
  //头部统计
  getDataTotal: function (year, month) {
    console.log(year + month)
    common._get('/wx/landlord/charge/month/totalfee/total', {year: year, month: month},
      //成功
      function (res) {
        if (res.data.success) {
          that.data.tabnav.tabitem[0].count = res.data.chargedNum;
          that.data.tabnav.tabitem[1].count = res.data.unchargedNum;
          that.setData({
            tabnav: that.data.tabnav
          })

        }
      });
  },

  /**
   * 点击每一项费用
   */
  chargesItemClick: function (e) {
    var chargeid = e.currentTarget.dataset.chargeid
    wx.navigateTo({
      url: '../receiptDetails/receiptDetails?chargeid=' + chargeid,
    })
  },
  //点击待租房间
  roomitemclick: function (e) {
    // var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '../waitingRentRoom/waitingRentRoom?room_id=' + id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that = this;
    this.getDataTotal(options.year, options.month);
    this.getfeeData(options.year, options.month,1);
    this.getfeeData(options.year, options.month, 0);
    that.setData({
      year: options.year,
      month: options.month
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //添加合同
    var addContract = wx.getStorageSync('addContract');
    if (addContract){
      this.getDataTotal(that.data.year, that.data.month);
      this.getfeeData(that.data.year, that.data.month, 1);
      this.getfeeData(that.data.year, that.data.month, 0);
    }
    if(app.getRefresh()){
      this.getDataTotal(that.data.year, that.data.month);
      this.getfeeData(that.data.year, that.data.month, 1);
      this.getfeeData(that.data.year, that.data.month, 0);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getDataTotal(that.data.year, that.data.month);
    this.getfeeData(that.data.year, that.data.month, 1);
    this.getfeeData(that.data.year, that.data.month, 0);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})